#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
class MovingAverage
{
public:
    MovingAverage(int size)
    {
        this->size = size;
        this->sum = 0.0;
    }

    double next(int val)
    {
        if (qu.size() == size)
        {
            sum -= qu.front();
            qu.pop();
        }
        qu.emplace(val);
        sum += val;
        return sum / qu.size();
    }

private:
    int size;
    double sum;
    queue<int> qu;
};

int main()
{   
    /**
     * @brief 
     * 队列，循环数组模拟队列
     * 
     */
    MovingAverage movingAverage = MovingAverage(3);
    cout << movingAverage.next(1) << endl;  // 返回 1.0 = 1 / 1
    cout << movingAverage.next(10) << endl; // 返回 5.5 = (1 + 10) / 2
    cout << movingAverage.next(3) << endl;  // 返回 4.66667 = (1 + 10 + 3) / 3
    cout << movingAverage.next(5) << endl;  // 返回 6.0 = (10 + 3 + 5) / 3

    system("pause");
    return 0;
}
